package cn.com.infcn.spider.web.collector;


import java.util.Calendar;
import java.util.List;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import cn.com.infcn.spider.SchedulerManager;
import cn.com.infcn.spider.URIManager;
import cn.com.infcn.spider.domain.Job;
import cn.com.infcn.spider.util.Constant;
import cn.com.infcn.spider.util.DateUtil;
import cn.com.infcn.spider.util.MongoDao;
import cn.com.infcn.spider.util.StaticValue;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;


/** 
* @Company: Beijing INFCN Software Co.,Ltd.
* @ClassName: StatAction 
* @Project: InfcnSpider 
* @Description: 统计相关类 
* @author jjs  
* @date 2015年9月25日 下午4:10:21 
* @version 1.0 
*/
@IocBean
public class StatAction {

	
	@Inject
	private MongoDao mongoDao;
	
	/** 
	* @Title: 查看Job详情 
	* @Description: 查看job的基本信息及任务统计信息 
	* @param @param jobName
	* @param @return    设定参数 
	* @return Object    返回类型 
	* @date 2015年9月25日 下午4:18:03 
	* @throws 
	*/
	@At("/admin/job/show/?")
	@Ok("jsp:/job_show.jsp")
	public Object show(String jobName) {

		DBObject findById = mongoDao.findById("job", jobName);

		if (findById == null) {
			return StaticValue.errMessageJson(jobName + " not find in db!");
		}
		findById.put("statusStr", Job.status2Str(findById.get("status")));
		
		//统计成功条数
		long succNum = mongoDao.count(URIManager.SUCCESS + jobName);
		//统计失败条数
		long errorNum = mongoDao.count(URIManager.ERROR + jobName);
		//统计总条数
		long totalNum = succNum+ errorNum;
		//待爬取任务数
		int taskCount = URIManager.uriQueue(jobName).size();
		//获取平均速率
		long[] speedPerMinute = getAverageSpeedRate(jobName);
		
		Mvcs.getReq().setAttribute("succNum", succNum);
		Mvcs.getReq().setAttribute("errorNum", errorNum);
		Mvcs.getReq().setAttribute("totalNum", totalNum);
		Mvcs.getReq().setAttribute("succRate", speedPerMinute[0]);
		Mvcs.getReq().setAttribute("errRate", speedPerMinute[1]);
		Mvcs.getReq().setAttribute("taskCount", taskCount);
		Mvcs.getReq().setAttribute("speedPerMinute", speedPerMinute[2]);
		Mvcs.getReq().setAttribute("_jobType", findById.get("jobType"));

		return findById;

	}
	/**
	 * 获取平均速率
	 * @param jobName
	 * @return
	 */
	public long[] getAverageSpeedRate(String jobName)
	{
		//任务时间差;
		long[] dateArray = null;
		//任务爬取信息
		long[] speedPerMinute = new long[3];
		//总共运行分钟数;
		long totalRunMinute = 0;
		//获取当前任务调度者;
		Job job = SchedulerManager.getJob(jobName);
		Object queryStr = "";
		if(job != null)
		{
			//获取时间差;
			dateArray = DateUtil.getTimeDifference(job.getStartTime(),Calendar.getInstance().getTime());
			if(dateArray[0] > 0)
			{
				totalRunMinute += dateArray[0] * 1440;//天
			}
			if(dateArray[1] > 0)
			{
				totalRunMinute += dateArray[1] * 60;//小时;
			}
			if(dateArray[2] > 0)
			{
				totalRunMinute += dateArray[2];//分;
			}else if(dateArray[2] == 0)
			{
				totalRunMinute = 1 ;
			}
			queryStr = job.getStartTime();
		}
		//查新爬取速率条件，
		DBObject query = new BasicDBObject("update_time",new BasicDBObject("$gte",queryStr));
		//查询最近一段时间内爬取成功的条数
		speedPerMinute[0] = mongoDao.getCollection(URIManager.SUCCESS + jobName).find(query).count();
		//查询最近一段时间内爬取失败的条数
		speedPerMinute[1] = mongoDao.getCollection(URIManager.ERROR + jobName).find(query).count();
		//爬取分钟速率（获取运行中抓取的数据求平均以确保数据均匀...)
		if(totalRunMinute > 0)
		{
			speedPerMinute[2] = (speedPerMinute[0]+speedPerMinute[1])/totalRunMinute;
		}
		return speedPerMinute;
	}
	/** 
	* @Title: 查看Job详情 
	* @Description: 查看job的基本信息及任务统计信息 
	* @param @param jobName
	* @param @return    设定参数 
	* @return Object    返回类型 
	* @date 2015年9月25日 下午4:18:03 
	* @throws 
	*/
	@At("/admin/job/dashboard")
	@Ok("jsp:/WEB-INF/dashboard/all_job_count_show.jsp")
	public Object dashboard() {
		
		DBObject jobType = new BasicDBObject("jobType",Constant.HTTP_JOB_TYPE);
		jobType.put("status",new BasicDBObject("$ne","-1"));
		List<DBObject> httpJobList = mongoDao.findByQuery("job",jobType);
		jobType.put("jobType",Constant.FTP_JOB_TYPE);
		List<DBObject> ftpJobList = mongoDao.findByQuery("job",jobType);
		long[] httpCountArray = getAlljobCountInfo(httpJobList);
		long[] ftpCountArray = getAlljobCountInfo(ftpJobList);
		//查询正在运行的任务;
		jobType.put("jobType",Constant.HTTP_JOB_TYPE);
		jobType.put("status", 1);
		List<DBObject> httpRunJobList = mongoDao.findByQuery("job",jobType);
		long allHttpRunSpeed = getAllRunJobAverageSpeed(httpRunJobList);
		//查询正在执行任务、未执行任务、完成任务的数量
		Mvcs.getReq().setAttribute("httpArray",httpCountArray);
		Mvcs.getReq().setAttribute("httpRunSpeed",allHttpRunSpeed);
		Mvcs.getReq().setAttribute("ftpArray",ftpCountArray);
		return null;
	}
	/**
	 * 获取所有正在运行任务的平均速率;
	 * @return
	 */
	public long getAllRunJobAverageSpeed(List<DBObject> list)
	{
		long averageSpeed = 0;
		int runSize = list.size() == 0 ? 1 : list.size();
		for(DBObject dObj : list)
		{
			String jobName = dObj.get("name").toString();
			averageSpeed += getAverageSpeedRate(jobName)[2];
		}
		return averageSpeed/runSize;
	}
	/**
	 * 获取任务统计信息;
	 * @param list
	 * @return
	 */
	public long[] getAlljobCountInfo(List<DBObject> list)
	{
		long[] jobCountArray = new long[4] ;
		for(DBObject dObj : list)
		{
			String jobName = dObj.get("name").toString();
			//统计成功条数
			long succNum = mongoDao.count(URIManager.SUCCESS + jobName);
			//统计失败条数
			long errorNum = mongoDao.count(URIManager.ERROR + jobName);
			jobCountArray[0] += succNum + errorNum;//总共抓取数量;
			jobCountArray[1] += succNum;//成功的数量;
			jobCountArray[2] += errorNum;//失败的数量;
			jobCountArray[3] += URIManager.uriQueue(jobName).size();//待爬任务数;
		}
		return jobCountArray;
	}
}
